package objects
{
	import flash.display.Sprite;
	import flash.geom.Point;

	/**
	 * @author robt
	 */
	public class Objects extends Sprite
	{
		private var _objects : Array;
		private var _yOffset : Number;

		public function Objects()
		{
			_objects = new Array();
		}

		public function addPlayer(position : Point) : void
		{
			var player : Player = new Player(_yOffset);

			player.x = position.x;
			player.y = position.y;

			_objects.push(player);

			update();
		}

		public function addPickup(position : Point) : void
		{
			var pickup : Pickup = new Pickup(_yOffset);

			pickup.x = position.x;
			pickup.y = position.y;

			_objects.push(pickup);

			update();
		}

		public function addMine(position : Point) : void
		{
			var mine : Mine = new Mine(_yOffset);

			mine.x = position.x;
			mine.y = position.y;

			_objects.push(mine);

			update();
		}

		public function addRepeller(position : Point) : void
		{
			var repeller : Repeller = new Repeller(_yOffset);

			repeller.x = position.x;
			repeller.y = position.y;

			_objects.push(repeller);

			update();
		}

		public function addAttractor(position : Point) : void
		{
			var attractor : Attractor = new Attractor(_yOffset);

			attractor.x = position.x;
			attractor.y = position.y;

			_objects.push(attractor);

			update();
		}

		public function addReactor(position : Point) : void
		{
			var reactor : Reactor = new Reactor(_yOffset);

			reactor.x = position.x;
			reactor.y = position.y;

			_objects.push(reactor);

			update();
		}

		public function addLaser(position : Point) : void
		{
			var laser : Laser = new Laser(_yOffset);

			laser.x = position.x;
			laser.y = position.y;
			laser.size = States.laserWidth;
			laser.horizontal = States.laserHorizontal;
			laser.onDuration = States.laserOn;
			laser.offDuration = States.laserOff;
			
			_objects.push(laser);

			update();
		}

		public function addReactorGravity(position : Point) : void
		{
			var reactor : ReactorGravity = new ReactorGravity(_yOffset);

			reactor.x = position.x;
			reactor.y = position.y;

			_objects.push(reactor);

			update();
		}

		public function addSwitchDoor(position : Point) : void
		{
			var switchDoor : SwitchDoor = new SwitchDoor(_yOffset);

			switchDoor.x = position.x;
			switchDoor.y = position.y;
			switchDoor.size = States.switchDoorWidth;
			switchDoor.vertical = !States.switchDoorHorizontal;
			switchDoor.open = States.switchDoorOpen;
			switchDoor.topLeftSwitch = States.switchDoorTopLeft;
			
			_objects.push(switchDoor);

			update();
		}
		
		public function addReactorDoor(position : Point) : void
		{
			var reaactorDoor : ReactorDoor = new ReactorDoor(_yOffset);

			reaactorDoor.x = position.x;
			reaactorDoor.y = position.y;
			reaactorDoor.size = States.reactorDoorWidth;
			reaactorDoor.horizontal = States.reactorDoorHorizontal;
			reaactorDoor.open = States.reactorDoorOpen;
			
			_objects.push(reaactorDoor);

			update();
		}

		public function addHatch(position : Point) : void {
			var hatch : Hatch = new Hatch(_yOffset);

			hatch.x = position.x;
			hatch.y = position.y;
			hatch.size = States.hatchSize;
			
			_objects.push(hatch);

			update();
		}

		private function update() : void
		{
			while (numChildren)
			{
				removeChildAt(0);
			}

			for each (var object : AbstractObject in _objects) {
				addChild(object);
			}
		}

		public function reset() : void
		{
			_objects = new Array();
			update();
		}

		public function undo() : void
		{
			_objects.pop();
			update();
		}

		public function getObjects() : Array
		{
			return _objects;
		}

		public function get yOffset() : Number
		{
			return _yOffset;
		}

		public function set yOffset(yOffset : Number) : void
		{
			_yOffset = yOffset;
		}
	}
}
